﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net; // Pour httpWebRequest
using System.IO; // pour Stream
using System.Diagnostics; // pour Process
using System.Threading;

using SeriesUpdatesCommon;

namespace SeriesUpdates
{
    public partial class MainWindow : Form
    {
        private List<CadreSerie> series = new List<CadreSerie>();
        private int serieThreadComplete;
        private BackgroundWorker bw;
        private List<Serie> seriesInDB;
        private DB _db;

        public MainWindow()
        {          
            InitializeComponent();
            this.serieThreadComplete = 0;
            this._db = DB.GetDB();

            ToolTip btnSearchTooltip = new ToolTip();
            btnSearchTooltip.SetToolTip(this.btn_search, "Rechercher");

            ToolTip btnEditTooltip = new ToolTip();
            btnEditTooltip.SetToolTip(btn_edit, "Modifier");

            if(!File.Exists("seriesUpdates.db3"))
            {
                this._db.CreateBase();
            }

            this._init();
        }

        private void _init()
        {
            this.seriesInDB = this._db.GetSeries();

            this.progressBar.Maximum = this.seriesInDB.Count * 100;

            if (this.seriesInDB.Count == 0)
            {
                this.btn_search.Enabled = false;
            }
            else
            {
                this.btn_search.Enabled = true;
            }
        }

        private void _clearAll()
        {
            panel.Controls.Clear();
            panelNoMaj.Controls.Clear();
            this.series.Clear();
            this.progressBar.Value = 0;
        }
        
        private void toggleButtons()
        {
            this.btn_edit.Enabled = !this.btn_edit.Enabled;
            this.btn_search.Enabled = !this.btn_search.Enabled;
            this.progressBar.Visible = !this.progressBar.Visible;
        }

        private void showAll()
        {
            this.serieThreadComplete = 0;

            foreach (CadreSerie serie in this.series)
            {
                this.bw = new BackgroundWorker();
                this.bw.DoWork += bw_DoWork;
                this.bw.RunWorkerCompleted += bw_RunWorkerCompleted;
                this.bw.RunWorkerAsync(serie);

                //Thread t = new Thread(() => serie.show());
                //t.IsBackground = true;
                //t.Start();
                //Thread.Sleep(20);
            }

            this.panel.Focus();
        }

        #region EVENTS
        private void btn_edit_Click(object sender, EventArgs e)
        {
            this.btn_edit.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);

            Edit frm_edit = new Edit();
            frm_edit.ShowDialog();            

            // On réinitialise après avoir modifié la liste des séries
            this._init();
        }

        public Panel getPanel()
        {
            return this.panel;
        }

        public Panel getPanelNoMaj()
        {
            return this.panelNoMaj;
        }

        public void refreshAndShowAll()
        {
            this._clearAll();
            this._init();

            if (seriesInDB.Count > 0)
            {
                foreach (Serie serie in seriesInDB)
                {
                    this.series.Add(new CadreSerie(this, serie.title, serie.url, serie.saison, serie.lastEpisodeNumber));
                }

                this.toggleButtons();
                this.showAll();
            }
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            this.refreshAndShowAll();
        }

        private void panelNoMaj_MouseEnter(object sender, EventArgs e)
        {
            panelNoMaj.Focus();
        }

        private void panel_MouseEnter(object sender, EventArgs e)
        {
            panel.Focus();
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            CadreSerie serie = e.Argument as CadreSerie;
            serie.createPanel();
        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Refresh();
            this.serieThreadComplete++;

            for(int i = 0; i < 100; i++){
                this.progressBar.Value += 1;

                Thread.Sleep(2);
            }

            if (this.serieThreadComplete == this.series.Count)
            {
                this.toggleButtons();
            }
            
        }
        #endregion
    }
}
